package charlie.shop.admin;

import charlie.shop.misc.Common;
import com.charlie.beans.Groups;
import com.charlie.db.DAOLocal;
import com.charlie.sessions.GroupsFacadeLocal;
import com.charlie.sessions.UsersFacadeLocal;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Delete user form group servlet
 * 
 * @author Оленка
 */
@WebServlet(name = "GroupDeleteUser", urlPatterns = {"/admin/groupDeleteUser"})
public class GroupDeleteUser extends HttpServlet {

    @EJB
    private GroupsFacadeLocal groupsFacade;
    @EJB
    private UsersFacadeLocal usersFacade;
    @EJB
    private DAOLocal dao;
    Groups group;

    /**
     * Process users and group information
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int gId = Common.getParametrInt(request, "groupId");
        group = groupsFacade.findByGroupsId(Integer.valueOf(gId));
        request.setAttribute("group", group);
        request.setAttribute("allUsers", dao.getUsersRange(1, usersFacade.count()));
        request.setAttribute("groupUsersId", dao.getUserIdByGroupId(gId));
        response.setContentType("text/html;charset=UTF-8");
        getServletContext().getRequestDispatcher("/admin/groupDeleteUser.jsp").forward(request, response);
    }

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Delete selected users from group
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String select[] = request.getParameterValues("id");
        if (select != null) {
            if (select.length != 0) {
                for (String uId : select) {
                    try {
                        int id = Integer.parseInt(uId);
                        dao.deleteUsersGroups(group.getGroupsId(), id);
                    } catch (Exception ex) {
                        Logger.getLogger(GroupDeleteUser.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
        processRequest(request, response);
    }   
}
